// FlowLayout.cc

/*
   Copyright 2006 Corey Tabaka

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

#include <FlowLayout.h>
#include <Container.h>

namespace gui {

FlowLayout::FlowLayout(void) {
	FlowLayout(FlowLayout::CENTER, 5, 5);
}

FlowLayout::FlowLayout(int align) {
	FlowLayout(align, 5, 5);
}

FlowLayout::FlowLayout(int align, int h, int v) {
	/*sd_printf("FlowLayout::constructor 3: align: %d, hgap: %d, vgap: %d\n",
		align, h, v);*/
	
	hgap = h;
	vgap = v;
	
	setAlignment(align);
}

FlowLayout::~FlowLayout(void) {
}

void FlowLayout::setAlignment(int a) {
	switch (a) {
		case LEADING:
			align = LEFT;
		break;
		case TRAILING:
			align = RIGHT;
		break;
		default:
			align = a;
		break;
	}
}

Dimension FlowLayout::getPreferredLayoutSize(RefContainer target) {
	Dimension dim(0, 0);
	
	int nmembers = target->getComponentCount();
	
	bool firstVisibleComponent = true;

	for (int i = 0 ; i < nmembers ; i++) {
		RefComponent m = target->getComponent(i);
		
		if (m->isVisible()) {
			Dimension d = m->getPreferredSize();
			dim.height = dim.height > d.height ? dim.height : d.height;
			
			if (firstVisibleComponent) {
				firstVisibleComponent = false;
			} else {
				dim.width += hgap;
			}
			
			dim.width += d.width;
		}
	}
	
	Insets insets = target->getInsets();
	dim.width += insets.left + insets.right + hgap*2;
	dim.height += insets.top + insets.bottom + vgap*2;
	
	return dim;
}

Dimension FlowLayout::getMinimumLayoutSize(RefContainer target) {
	Dimension dim(0, 0);
	
	int nmembers = target->getComponentCount();
	
	bool firstVisibleComponent = true;

	for (int i = 0 ; i < nmembers ; i++) {
		RefComponent m = target->getComponent(i);
		
		if (m->isVisible()) {
			Dimension d = m->getMinimumSize();
			dim.height = dim.height > d.height ? dim.height : d.height;
			
			if (firstVisibleComponent) {
				firstVisibleComponent = false;
			} else {
				dim.width += hgap;
			}
			
			dim.width += d.width;
		}
	}
	
	Insets insets = target->getInsets();
	dim.width += insets.left + insets.right + hgap*2;
	dim.height += insets.top + insets.bottom + vgap*2;
	
	return dim;
}

void FlowLayout::layoutContainer(RefContainer target) {
	//sd_printf("FlowLayout::layoutContainer\n");
	
	Insets insets = target->getInsets();
	int maxwidth = target->getWidth() - (insets.left + insets.right + hgap*2);
	int nmembers = target->getComponentCount();
	int x = 0, y = insets.top + vgap;
	int rowh = 0, start = 0;
	
	bool ltr = true; //target.getComponentOrientation().isLeftToRight();
	
	for (int i = 0 ; i < nmembers ; i++) {
		RefComponent m = target->getComponent(i);
		
		if (m->isVisible()) {
			Dimension d = m->getPreferredSize();
			m->setSize(d.width, d.height);
			
			if ((x == 0) || ((x + d.width) <= maxwidth)) {
				if (x > 0) {
					x += hgap;
				}
				
				x += d.width;
				rowh = rowh > d.height ? rowh : d.height;
			} else {
				moveComponents(target, insets.left + hgap, y, maxwidth - x, rowh, start, i, ltr);
				
				x = d.width;
				y += vgap + rowh;
				rowh = d.height;
				start = i;
			}
		}
	}
	
	moveComponents(target, insets.left + hgap, y, maxwidth - x, rowh, start, nmembers, ltr);
}

void FlowLayout::moveComponents(RefContainer target, int x, int y, int width, int height,
	int rowStart, int rowEnd, bool ltr) {
	switch (align) {
		case LEFT:
			x += ltr ? 0 : width;
		break;
		
		case CENTER:
			x += width / 2;
		break;
		
		case RIGHT:
			x += ltr ? width : 0;
		break;
		
		case LEADING:
		break;
		
		case TRAILING:
			x += width;
		break;
	}
	
	for (int i = rowStart ; i < rowEnd ; i++) {
		RefComponent m = target->getComponent(i);
		
		if (m->isVisible()) {
			if (ltr) {
				m->setLocation(x, y + (height - m->getHeight()) / 2);
			} else {
				m->setLocation(target->getWidth() - x - m->getWidth(),
					y + (height - m->getHeight()) / 2);
			}
			
			x += m->getWidth() + hgap;
		}
	}
}

} // namespace gui
